package com.hualan.service.b_yizhan;

import com.alibaba.fastjson.JSON;
import com.hualan.entity.QiShou;
import com.hualan.entity.Result;
import com.hualan.entity.ResultDate;
import com.hualan.entity.YiZhan;
import com.hualan.utils.EntityFactory;
import com.hualan.utils.ExecUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@WebServlet(urlPatterns = {"/panghudaojiaBackend/b_yizhan/yizhanUpdate","/panghudaojiaBackend/b_yizhan/yizhanList"})
public class YiZhanService extends HttpServlet {
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String location = req.getServletPath();
        if (location.equals("/panghudaojiaBackend/b_yizhan/yizhanUpdate")) {
            int flag = Integer.parseInt(req.getParameter("addOrUpdateFlag"));
            if (flag == 1) {
                //添加驿站
                String sql = "insert into b_yizhan values(null,?,?,?,?,?,?,?,0,?,1)";
                List<Object> list = new ArrayList<>();
                list.add(req.getParameter("courierStationName"));
                list.add(req.getParameter("courierStationImage"));
                list.add(req.getParameter("courierStationProvince"));
                list.add(req.getParameter("courierStationCity"));
                list.add(req.getParameter("courierStationDistrict"));
                list.add(req.getParameter("courierStationAddress"));
                list.add(req.getParameter("courierStationCommunityName"));
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                list.add(LocalDateTime.now().format(formatter));

                ExecUtil.execute(sql, list, req, true);
                ExecUtil.commit();
                ExecUtil.close();
                resp.getWriter().write(JSON.toJSONString(new Result(200, "增加成功！")));
            } else if (flag == 2) {
                //修改驿站
                String sql = "update b_yizhan set courierStationName=?,courierStationImage=?,courierStationProvince=?,courierStationCity=?,courierStationDistrict=?,courierStationAddress=?,courierStationCommunityName=?,courierStationStatus=? where courierStationId = ?;";
                List<Object> list = new ArrayList<>();
                list.add(req.getParameter("courierStationName"));
                list.add(req.getParameter("courierStationImage"));
                list.add(req.getParameter("courierStationProvince"));
                list.add(req.getParameter("courierStationCity"));
                list.add(req.getParameter("courierStationDistrict"));
                list.add(req.getParameter("courierStationAddress"));
                list.add(req.getParameter("courierStationCommunityName"));
                list.add(req.getParameter("courierStationStatus"));
                list.add(req.getParameter("courierStationId"));
                ExecUtil.execute(sql, list, req, true);
                ExecUtil.commit();
                ExecUtil.close();
                resp.getWriter().write(JSON.toJSONString(new Result(200, "修改成功！")));
            }
        }
    }
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //查询骑手数据
            String location = req.getServletPath();
            if(location.equals("/panghudaojiaBackend/b_yizhan/yizhanList")){
                String sql = "select * from b_yizhan where ";
                List<Object> list = new ArrayList<>();//装外界传的参数
                if(!Objects.equals(req.getParameter("yizhanname"), "")){
                    sql += "courierStationName like ? and ";
                    list.add("%"+req.getParameter("yizhanname")+"%");
                }
                if(!Objects.equals(req.getParameter("xiaoquname"), "")){
                    sql += "courierStationCommunityName like ? and ";
                    list.add("%"+req.getParameter("xiaoquname")+"%");
                }
                if(!Objects.equals(req.getParameter("startTime"), "") && !Objects.equals(req.getParameter("endTime"), "")){
                    sql += "addTime between ? and ? and ";
                    list.add(req.getParameter("startTime"));
                    list.add(req.getParameter("endTime"));
                }
                sql+="\'a\'=\'a\' ";
                System.out.println(sql);
                Integer pageNum = Integer.parseInt(req.getParameter("pageNum"));
                Integer pageSize = Integer.parseInt(req.getParameter("pageSize"));
                Integer start = pageSize*(pageNum-1);
                Integer end = pageSize*pageNum;
                sql += "limit "+start+","+end;
                ResultSet resultSet =(ResultSet) ExecUtil.execute(sql,list,req,false);
                ExecUtil.commit();
                List<Object> yizhans = new ArrayList<>();
                try{
                    while (resultSet.next()){
                        EntityFactory entityFactory = new EntityFactory(YiZhan.class);
                        YiZhan yiZhan =(YiZhan) entityFactory.startCombine(resultSet);
                        yizhans.add(yiZhan);
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                ExecUtil.close();
                sql = "select count(courierStationId) total from b_yizhan";
                list.clear();
                ResultSet resultSet1 = (ResultSet) ExecUtil.execute(sql,list,req,false);
                ExecUtil.commit();
                int total = 0;
                try {
                    while (resultSet1.next()){
                        total = resultSet1.getInt(1);
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                ExecUtil.close();
                resp.getWriter().write(JSON.toJSONString(new Result(200,new ResultDate(pageNum,pageSize,total,total/pageSize+(total%pageSize!=0?1:0),yizhans))));
            }






    }
}
